﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

namespace WindowsPhoneApplication1
{
    public partial class LoginPage : PhoneApplicationPage
    {
        WindowsPhoneApplication1.ServiceReference1.Service1Client client;
        public LoginPage()
        {
            InitializeComponent();
            client = new WindowsPhoneApplication1.ServiceReference1.Service1Client();
            client.LoginUserCompleted += new EventHandler<ServiceReference1.LoginUserCompletedEventArgs>(client_LoginUserCompleted);
        }

        void client_LoginUserCompleted(object sender, ServiceReference1.LoginUserCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != "")
            {
                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                if (!settings.Contains("userData"))
                {
                    settings.Add("userData", e.Result);
                }
                else
                {
                    settings["userData"] = e.Result;
                }
                settings.Save();
                this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("Could not log in.\nPlease check username/password.");
            }
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void usernameBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (usernameBox.Text.Equals("Username"))
                usernameBox.Text = "";

        }

        private void usernameBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (usernameBox.Text.Equals(""))
                usernameBox.Text = "Username";
        }

        private void passwordBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (passwordBox.Password.Equals(""))
                passwordBoxTxt.Visibility = Visibility.Visible;
        }

        private void passwordBoxTxt_GotFocus(object sender, RoutedEventArgs e)
        {
            passwordBoxTxt.Visibility = Visibility.Collapsed;
            passwordBox.Focus();
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            string username = usernameBox.Text;
            string password = passwordBox.Password;
            if (username.Equals("") || password.Equals(""))
            {
                MessageBox.Show("Username Or Password Required");
                passwordBoxTxt.Visibility = Visibility.Visible;
            }
            else
                client.LoginUserAsync(username, password);

        }
    }
}